class File{
	constructor(){
		this.root=null
	}
	/**
	 * 处理文件夹获取全部文件和文件夹
	 * @param {showDirectoryPicker返回值} dirHeader 
	 * @returns Array
	 */
	async dirTree(dirHeader){
		const entries=dirHeader.entries()
		let entriesResult=await entries.next()
		const dir=[]
		while(entriesResult.done===false){
			const value=entriesResult.value[1]
			const obj={
				type:value.kind,
				name:value.name,
				open:false
			}
			if(obj.type==="file"){
				obj.file=value
			}else{
				obj.dir=value
				obj.folderContents=await this.dirTree(value)
			}
			dir.push(obj)
			entriesResult=await entries.next()
		}
		return dir
	}
	async fileTree(dir){
		
	}
	async showDir(){
		let dir=null
		let dirArr=null
		try {
			dir=await window.showDirectoryPicker({
				id:0,
				mode:"read"||"readwrite",
				//默认打开的目录如果有id，id优先
				// startIn:"desktop"||"documents"||"downloads"||"music"||"pictures"||"videos"
			})
			console.log("dir:",dir);
			dirArr=await this.dirTree(dir)
			this.root=dir
		} catch (error) {
			console.error("取消打开:",error);
		}
		// const handle = await self.showOpenFilePicker()
		// dir.resolve(handle[0]).then(res=>{//传一个FileSystemHandle返回路径文件夹数组
		// 	console.log("res:",res);
		// })
		console.log("dirArr:",dirArr);
		return dirArr
	}
	getFile(){

	}

	/**
	 * getFileContent 1.0
	 * @param {FileSystemFileHandle} f 
	 * @returns {code:200|0,data:String}
	 */
	getFileContent(f){
		//可以加个缓存
		//判断文件类型进行内容加载,目前只能加载文本
		// type:"image/jpeg"
		// type: "text/plain"
		// type: "application/json"
		const result={
			code:200,
			data:"出问题了！大概率是因为这不是一个文件"
		}
		if(f&&f.getFile){
			return new Promise(r=>{
				f.getFile().then(file=>{
					console.log("file:",file);
					const reader = new FileReader();
					reader.readAsText(file);
					reader.onload =function(evt) {
						result.code=0
						result.data=evt.target.result
						r(result)
					};
				})
			})
		}else{
			return result
		}
	}
	async saveDir(dir,name){
		return await dir.getDirectoryHandle(name, {create: true});
	}
	async saveFile(dir,name){
		// const opts = {
		// 	excludeAcceptAllOption:false,//不知道作用
		// 	suggestedName:"文件名",
		// 	types: [{
		// 		description: 'Text file',
		// 		accept: {'txt/plain': ['.txt']},
		// 	},{
		// 		description: '说明',
		// 		accept: {'text/json': ['.json']},
		// 	}],
		// };
		// return await window.showSaveFilePicker(opts);
		const fileHandle = dir.getFileHandle(name, {create: true});
		console.log("fileHandle:",fileHandle);
		return fileHandle
	}
}

// async function* getFilesRecursively (entry) {//留着学习
// 	if (entry.kind === 'file') {
// 	  const file = await entry.getFile();
// 	  if (file !== null) {
// 		file.relativePath = getRelativePath(entry);
// 		yield file;
// 	  }
// 	} else if (entry.kind === 'directory') {
// 	  for await (const handle of entry.values()) {
// 		yield* getFilesRecursively(handle);
// 	  }
// 	}
//   }
// async function writeFile(fileHandle, contents) {
// 	// Create a FileSystemWritableFileStream to write to.
// 	const writable = await fileHandle.createWritable();
  
// 	// Write the contents of the file to the stream.
// 	await writable.write(contents);
  
// 	// Close the file and write the contents to disk.
// 	await writable.close();
//   }
export const file=new File()